﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.LogManage;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.LogManage.Login
{
    public class LoginLogServeice : ILoginLogServeice
    {
        private readonly IRepository<LoginLog> _studentService;

        public LoginLogServeice(IRepository<LoginLog> studentService)
        {
            _studentService = studentService;
        }

        public async Task<Response<List<LoginLog>>> GetLoginLog(Lmodel lmodel)
        {
            var obj = await _studentService.GetAllListAsync();


            if (!string.IsNullOrWhiteSpace(lmodel.userName))
            {
                obj = obj.Where(x => x.UserName.Contains(lmodel.userName)).ToList();
            }

            if (!string.IsNullOrEmpty(lmodel.isDelete))
            {
                if (lmodel.isDelete == "true")
                {
                    obj = obj.Where(x => x.IsDelete == true).ToList();
                }
                else
                {
                    obj = obj.Where(x => x.IsDelete == false).ToList();
                }
            }

            if (lmodel.station.HasValue && lmodel.enddate.HasValue)
            {
                obj = obj.Where(x => x.LoginDate > lmodel.station && x.LoginDate < lmodel.enddate).ToList();
            }

            //总条数
            int total = obj.Count();
            //分页
            obj = obj.Skip((lmodel.pageIndex - 1) * lmodel.pageSize).Take(lmodel.pageSize).ToList();

            var result = obj.MapToList<LoginLog>();
            return Response<List<LoginLog>>.Ok(result, total);
        }
    }

    public class Lmodel
    {
        public int pageIndex { get; set; }

        public int pageSize { get; set; }
        public string? userName { get; set; }

        public string? isDelete { get; set; }

        public DateTime? station { get; set; }

        public DateTime? enddate { get; set; }
    }
}
